package com.second.controller.loginandregister;

import com.aliyuncs.exceptions.ClientException;
import com.second.consts.RabbitConst;
import com.second.mapper.FUserMapper;
import com.second.model.pojo.FUser;
import com.second.moudle.Result;
import com.second.util.SmsUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("index")
public class RegisterController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FUserMapper userMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("register")
    @ResponseBody
    public Result register(@RequestBody Map<String,Object> user) {
        String phone = (String) user.get("phone");
        String code = (String) user.get("code");
        String password = (String) user.get("password");
        String passwordAgain = (String) user.get("passwordAgain");
//        System.out.println(phone+":"+code+":"+password);
        FUser user1 = userMapper.findByTelNumber(phone);
        if (user1 != null) {
            return new Result("401", "sorry,此号码已被注册存在");
        }
        String phoneCode = redisTemplate.opsForValue().get(phone).toString();
        if (null != code && null!=password && password.trim().length()>0 && code.equals(phoneCode) && password.equals(passwordAgain)) {
            FUser fuser = new FUser();
            fuser.setUsername(phone);
            fuser.setTelNumber(phone);
            fuser.setRole(1);
            fuser.setPassword(password);
            fuser.setHeadPicture("recommend1");
            userMapper.save(fuser);
            return new Result("200", "注册成功", fuser);
        }else if (!password.equals(passwordAgain)){
            return new Result("300", "两次密码不一致");
        } else if (!code.equals(phoneCode)){
            return new Result("400", "验证码错误");
        }else if (password.trim().length()==0 || passwordAgain.trim().length()==0){
            return new Result("301", "密码不能为空");
        }else return null;

    }


    /**
     * 用户注册发送短信
     * @param phone
     * @return
     * @throws ClientException
     */
    @PostMapping("sms")
    @ResponseBody
    public Result phoneLogin(String phone) throws ClientException {
        FUser user = userMapper.findByTelNumber(phone);
        System.out.println(user);
        if (null == user && null!=phone) {
            String code = SmsUtils.code(6);
//            SmsUtils.sendSms(phone, code);
            Map<String, String> msgMap = new HashMap<>();
            msgMap.put("phone", phone);
            msgMap.put("code", code);
            ValueOperations ops = redisTemplate.opsForValue();
            ops.set(phone, code, 180, TimeUnit.SECONDS);
            rabbitTemplate.convertAndSend(RabbitConst.SIMPLE_QUEUE, msgMap);
            return new Result("200", "发送成功", code);
        } else {
            return new Result("400", "此手机号已被注册");
        }
    }
}
